//定期更新用户密码，以达到更新盐值的目的
	@Component
	public class PasswordUpdateTask {

		private final Logger logger = LoggerFactory.getLogger(PasswordUpdateTask.class);

		@Autowired
		private UserService userService;

		//@Value("${user.default.password}")//此处需注意不同用户应当根据其用户名等信息生成密码，并进行保存
		//private String defaultPassword;

		@Value("${user.password.file.path}")
		private String passwordFilePath;


		@Scheduled(fixedRate = 259200000) //259200000// 每3天执行一次，单位为毫秒，可修改，实际工作中应当6个月为一个周期
		public void updatePasswords() {
			List<User> users = userService.findAllUsers();
			try (BufferedWriter writer = new BufferedWriter(new FileWriter(passwordFilePath, true))) {
				for (User user : users) {
					// 根据用户名生成唯一的密码
					String uniquePassword = generatePassword(user.getLoginname());
					byte[] salt = SM3SaltHelper.generateSalt();
					String newSaltHex = SM3SaltHelper.bytesToHex(salt);
					String encryptedPassword = SM3SaltHelper.encrypt(uniquePassword, salt);
					// 使用实例方法来查找附件
					List<Attachment> attachments = attachmentService.findAttachmentsByUserId(user.getUserid());

					for (Attachment attachment : attachments) {
						String decryptedOldKey = new String(attachment.getEncodekey(user), StandardCharsets.UTF_8);
						String newEncodedKey = SM4Tools.encrypt(decryptedOldKey, SM3SaltHelper.getEncodeKey(encryptedPassword,newSaltHex).substring(0,16));//encryptedPassword.substring(0, 16));

						attachment.setEncodekey(newEncodedKey);
						attachmentService.updateAttachment(attachment);
					}

					user.setPassword(encryptedPassword);
					user.setSalt(SM3SaltHelper.bytesToHex(salt));
					boolean isUpdated = userService.updateUser1(user);
					if (isUpdated) {
						logger.info("用户 {} 密码成功更新。新密码： {}", user.getLoginname(), encryptedPassword);
						// 将用户名和密码写入文件
						writer.write("用户名: " + user.getLoginname() + ", 密码: " + uniquePassword);
						writer.newLine();
					} else {
						logger.error("更新用户 {} 失败.", user.getLoginname());
					}
				}
			} catch (IOException e) {
				logger.error("写入文件时发生错误: ", e);
			} catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

		private String generatePassword(String username) {
			// 这里可以使用任何你喜欢的算法来生成密码
			// 例如，简单地将用户名反转并附加一些固定字符串
			return new StringBuilder(username).reverse().toString() + "123";
		}
	}